package com.heima.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.seckill.exception.GlobalException;
import com.heima.seckill.mapper.UserMapper;
import com.heima.seckill.pojo.User;
import com.heima.seckill.service.IUserService;
import com.heima.seckill.utils.CookieUtil;
import com.heima.seckill.utils.MD5Util;
import com.heima.seckill.utils.UUIDUtil;
import com.heima.seckill.vo.LoginVo;
import com.heima.seckill.vo.RespBean;
import com.heima.seckill.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhujia
 * @since 2022-04-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Qualifier("JsonRedisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletResponse response, HttpServletRequest request) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        User user = userMapper.selectById(mobile);
        if (user==null){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        if (!MD5Util.formPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        redisTemplate.opsForValue().set("user:"+ticket,user);
//        request.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(request,response, "userTicket" , ticket) ;
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request,HttpServletResponse response) {
        if ( StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user!=null){ //持久化cookie
            CookieUtil.setCookie(request,response, "userTicket" , userTicket) ;
        }
        return user;
    }

    @Override
    public RespBean userUpdate(String userTicket, String password,HttpServletRequest request,HttpServletResponse response) {
        User user =getUserByCookie(userTicket, request, response);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE__NOT_EXIST);
        }
        user . setPassword (MD5Util. inputPassToDBPass (password, user.getSlat()));
        int result = userMapper.updateById(user);
        if (1 == result) {
               // 删除Redis
            redisTemplate. delete( "user:" + userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum .PASSWORD_UPDATE_FAIL);
    }
}
